package com.ld.admin.controller.usercenter.countdown;

import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.countdown.SysUserCountdownModel;
import com.ld.shieldsb.annotation.duplicate.SubmitRequestIntercept;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.PageNavigationBean;
import com.ld.shieldsb.dao.model.QueryModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 倒计时牌
 * 
 * @ClassName SysUserCountdownController
 * @author 刘金浩
 * @date 2019年09月02日 16:08:53
 *
 */
@Api(tags = "倒计时牌")
@Controller(value = "countdownSysUserCountdownController")
@RequestMapping("${shieldsb.adminPath}/usercenter/user_countdown")
public class SysUserCountdownController extends AdminBasicController<SysUserCountdownModel> {

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        return "forward:" + adminPath + "/usercenter/user_countdown/toSearch"; // 跳转搜索页面
    }

    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "/toSearch", method = RequestMethod.GET)
    public String toSearch() {
        String nowDate = DateUtil.getDateStr(new Date(), "yyyy年MM月dd日") + " " + DateUtil.getWeekOfDate(new Date());
        request.setAttribute("nowDate", nowDate);
        return "a/usercenter/countdown/sysusercountdown_search";
    }

    @ApiOperation(value = "搜索结果", notes = "")
    @RequestMapping(path = "/search", method = RequestMethod.POST)
    public String search() {
        QueryModel queryModel = basicService.getSearchQueryModel(request, getModelClass());
        queryModel.addEq(SysUserCountdownModel::getCreateBy, Web.Util.getSessionUserId(request));
        queryModel.setOrder("xh desc,createTime ", true);
        int pageNum = getInt("p");
        PageNavigationBean<SysUserCountdownModel> pageBean = basicService.getPageBean(request, getModelClass(), queryModel, pageNum, 15); // 每页显示15条
        setCountdownBackGround(pageBean.getResultList());
        return "a/usercenter/countdown/sysusercountdown_list";
    }

    /**
     * 
     * 设置倒计时牌背景色
     * 
     * @Title setCountdownBackGround
     * @author 刘金浩
     * @date 2019年9月3日 上午10:46:40
     * @param list
     *            void
     */
    public void setCountdownBackGround(List<SysUserCountdownModel> list) {
        list.forEach(model -> {
            model.setTimeModel(DateUtil.dateDiff(new Date(), model.getEndTime()));
            model.setEndTimeStr(DateUtil.getDateStr(model.getEndTime(), "yyyy年MM月dd日") + " " + DateUtil.getWeekOfDate(model.getEndTime()));
            model.setCountDownTimeStr(DateUtil.getDateStr(model.getEndTime(), "yyyy/MM/dd"));
            Result result = DateUtil.isDayInfo(model.getEndTime(), 3);// 如果是3天，背景变成橙色
            if (result.getSuccess()) {
                result = DateUtil.isDayInfo(model.getEndTime(), 1);// 如果是1天，背景变成红色
                if (result.getSuccess()) {
                    model.setDeplyBackgroundColor("timered");
                } else {
                    int date = (int) result.getData();
                    if (date == -1) {
                        // 灰色
                        model.setDeplyBackgroundColor("timegray");
                    } else {
                        model.setDeplyBackgroundColor("timeyellow");
                        // 橙色timeyellow
                    }
                }
            } else {
                int date = (int) result.getData();
                if (date == -1) { // 如果是过期，背景变成灰色
                    // 灰色
                    model.setDeplyBackgroundColor("timegray");
                }
            }
        });
    }

    @ApiOperation(value = "倒计时牌展示", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        long id = getLong("id");
        basicService.getOne(request, getModelClass(), id);
        return "a/usercenter/countdown/sysusercountdown_show";
    }

    @ApiOperation(value = "跳转到添加/修改倒计时牌", notes = "")
    @RequestMapping(path = "/toAddOrUpdate", method = RequestMethod.GET)
    public String toAddOrUpd() {
        long id = getLong("id");
        basicService.getOne(request, getModelClass(), id);
        return "a/usercenter/countdown/sysusercountdown_update";
    }

    @ApiOperation(value = "添加/修改倒计时牌", notes = "")
    @RequestMapping(path = "/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object addOrUpd(@Valid SysUserCountdownModel model) {
        Result result = basicService.addOrUpdate(model, model.getId() != null);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "删除倒计时牌", notes = "")
    @RequestMapping(path = "/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object del() {
        long id = getLong("id");
        Result result = basicService.remove(getModelClass(), id);
        return Web.Response.result(result);
    }

}